package com.nf.mvc.view;


import com.fasterxml.jackson.databind.ObjectMapper;
import com.nf.mvc.ViewResult;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class JsonViewResult extends ViewResult {

    private Object data;

    public JsonViewResult(Object data) {
        this.data = data;
    }

    @Override
    public void render(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        resp.setContentType("application/json;charset=UTF-8");

        ObjectMapper objectMapper = new ObjectMapper();
        String json = objectMapper.writeValueAsString(data);
        resp.getWriter().print(json);
    }
}
